package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.cache.redis.service.IStrategyStatisVOService;
import cn.wolfcode.wolf2w.cache.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.interceptor.RequiredLogin;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserInfoController {

    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.appkey}")
    private String appkey;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVOService strategyStatisVOService;

    @GetMapping("list")
    public Object list(){
        return userInfoService.list();
    }

    @GetMapping("checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone , smsUrl , appkey);
        return JsonResult.success();
    }

    @PostMapping("regist")
    public Object regist(String phone , String nickname , String password , String rpassword , String verifyCode){
        userInfoService.regist(phone , nickname , password , rpassword , verifyCode);
        return JsonResult.success();
    }

    @PostMapping("login")
    public Object login(String username , String password){
        // 1.登录成功返回用户对象
        UserInfo userInfo = userInfoService.login(username , password);
        // 2.创建并把token存入redis缓存
        String token = userInfoRedisService.setToken(userInfo);
        // 3.将当前用户对象以及token直接返回
        Map<String , Object> map = new HashMap<>();
        map.put("token" , token);
        map.put("user" , userInfo);

        return JsonResult.success(map);
    }

    @RequiredLogin
    @PostMapping("currentUser")
    public Object currentUser(HttpServletRequest request){
        // 页面发过来的请求,会把token放入请求头,现在从请求头中获取token
        String token = request.getHeader("token");
        // 通过token 获取代码对象
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }

    @RequiredLogin
    @GetMapping("strategies/favor")
    public Object favor(String sid , String userId){
        List<String> list = strategyStatisVOService.userStrategyFvor(sid , userId);
        return JsonResult.success(list);
    }


}
